package com.lc.yygh.hosp.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.lc.yygh.cmnclient.DictFeignClient;
import com.lc.yygh.hosp.repository.HospitalRepository;
import com.lc.yygh.hosp.service.DepartmentService;
import com.lc.yygh.hosp.service.HospitalService;
import com.lc.yygh.model.hosp.BookingRule;
import com.lc.yygh.model.hosp.Hospital;
import com.lc.yygh.vo.hosp.HospitalQueryVo;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.*;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class HospitalServiceImpl  implements HospitalService {
    @Autowired
    private HospitalRepository hospitalRepository;

    //注入远程调用的接口，就能通过它调用数据字典服务
    @Autowired
    private DictFeignClient dictFeignClient;



    @Override
    public void save(Map<String, Object> paramMap) {//paramMap接收的是hospital的信息
        Hospital hospital=JSONObject.parseObject(JSONObject.toJSONString(paramMap),Hospital.class);//先将map中的医院信息转化成JSON数据，再封装成一个对象
        //根据医院编码去查询得到一个Hospital
        Hospital targetHospital = hospitalRepository.findByHoscode(hospital.getHoscode());
        if (targetHospital==null){
            //数据库中不存在 做添加操作
            hospital.setStatus(0);
            Date date = new Date();
            hospital.setCreateTime(date);
            hospital.setUpdateTime(date);
            hospital.setIsDeleted(0);
            hospitalRepository.save(hospital);
        }else {
            //数据库存在，做更新操作
            targetHospital.setUpdateTime(new Date());
            targetHospital.setIsDeleted(0);
            hospitalRepository.save(targetHospital);
        }
    }

    @Override
    public Hospital findHospital(String hoscode) {
        Hospital hospital = hospitalRepository.findByHoscode(hoscode);
        return hospital;
    }

    @Override
    public Page<Hospital> selectPage(Integer page, Integer limit, HospitalQueryVo hospitalQueryVo) {
        Sort sort = Sort.by(Sort.Direction.DESC, "createTime");
        //0为第一页
        Pageable pageable = PageRequest.of(page-1, limit, sort);

        Hospital hospital = new Hospital();
        BeanUtils.copyProperties(hospitalQueryVo, hospital);

        //创建匹配器，即如何使用查询条件
        ExampleMatcher matcher = ExampleMatcher.matching() //构建对象
                .withStringMatcher(ExampleMatcher.StringMatcher.CONTAINING) //改变默认字符串匹配方式：模糊查询
                .withIgnoreCase(true); //改变默认大小写忽略方式：忽略大小写

        //创建实例
        Example<Hospital> example = Example.of(hospital, matcher);
        Page<Hospital> pages = hospitalRepository.findAll(example, pageable);
        for (Hospital hosp : pages) {
            //封装Hospital对象(写成了一个方法)
            this.packHospital(hosp);
        }
        return pages;
    }



    private Hospital packHospital(Hospital hosp){
        //调用DictFeignClient中的远程调用方法获取省市名称
        String provName = dictFeignClient.getName(hosp.getProvinceCode());
        String cityName = dictFeignClient.getName(hosp.getCityCode());
        String districtName = dictFeignClient.getName(hosp.getDistrictCode());
        //获取医院等级名称
        String hostypeName = dictFeignClient.getName("Hostype", hosp.getHostype());
        //医院类里有一个Map属性，就是为了存取一些需要的数据
        hosp.getParam().put("hostypeName",hostypeName);
        hosp.getParam().put("fullAddress",provName+cityName+districtName);
        return hosp;

    }

    //更新医院上线状态
    @Override
    public void updateStatus(String id, Integer status) {
        if(status.intValue() == 0 || status.intValue() == 1) {
            Hospital hospital = hospitalRepository.findById(id).get();
            hospital.setStatus(status);
            //设置更新时间为当前时间
            hospital.setUpdateTime(new Date());
            //更新Mongobd对应的文档
            hospitalRepository.save(hospital);
        }
    }

    @Override
    public Map<String, Object> getDetail(String id) {//id是Mongodb里的文档的字符串，不是Integer
        Hospital hospital = hospitalRepository.findById(id).get();
        BookingRule bookingRule = hospital.getBookingRule();
        //创建Map存储数据
        Map<String,Object>map=new HashMap<>();
        map.put("hospital",this.packHospital(hospital));
        map.put("bookingRule",bookingRule);
        //不需要重复返回
        hospital.setBookingRule(null);
        return map;
    }

    @Override
    public String getHospName(String hoscode) {
        Hospital hos = hospitalRepository.findByHoscode(hoscode);
        return hos.getHosname();
    }

    @Override
    public List<Hospital> findByHosname(String hosname) {
        List<Hospital> hosList = hospitalRepository.findByHosnameLike(hosname);

        return hosList;
    }

    @Override
    public Map<String, Object> item(String hoscode) {
        //根据hoscode获取医院
        Hospital hosp = hospitalRepository.findByHoscode(hoscode);
        hosp=this.packHospital(hosp);
        Map<String,Object>map=new HashMap<>();
        map.put("hospital",hosp);
        map.put("bookingRule",hosp.getBookingRule());
        //不需要重复返回
        hosp.setBookingRule(null);
        return map;
    }

}
